﻿using System.Data;
using VMukti.Common;
using VMukti.DataAccess.VMuktiGrid;
using System.Data.SqlClient;
using System.Collections.Generic;
using VMukti.Business.CommonDataContracts;
using VMuktiAPI;
using System;
//using VMukti.Business.CommonDataContracts;

namespace VMukti.Business.VMuktiGrid
{
    public class ClsTab : ClsBaseObject
    {
        #region Fields

        private int intTabId = VMukti.Common.ClsConstants.NullInt;
        private int intPageId = VMukti.Common.ClsConstants.NullInt;
        private int intTabPosition = VMukti.Common.ClsConstants.NullInt;
        private string strTabTitle = VMukti.Common.ClsConstants.NullString;
        private string strTabDesc = VMukti.Common.ClsConstants.NullString;
        private double dblC1Width = VMukti.Common.ClsConstants.NullDouble;
        private double dblC2Width = VMukti.Common.ClsConstants.NullDouble;
        private double dblC3Width = VMukti.Common.ClsConstants.NullDouble;

        #endregion

        #region Properties

        public int TabId
        {
            get { return intTabId; }
            set { intTabId = value; }
        }

        public int PageId
        {
            get { return intPageId; }
            set { intPageId = value; }
        }

        public int TabPosition
        {
            get { return intTabPosition; }
            set { intTabPosition = value; }
        }

        public string TabTitle
        {
            get { return strTabTitle; }
            set { strTabTitle = value; }
        }

        public string TabDesc
        {
            get { return strTabDesc; }
            set { strTabDesc = value; }
        }

        public double C1Width
        {
            get { return dblC1Width; }
            set { dblC1Width = value; }
        }

        public double C2Width
        {
            get { return dblC2Width; }
            set { dblC2Width = value; }
        }

        public double C3Width
        {
            get { return dblC3Width; }
            set { dblC3Width = value; }
        }

        #endregion

        #region Methods

        public override bool MapData(DataRow row)
        {
            try
            {
                intTabId = GetInt(row, "ID");
                intPageId = GetInt(row, "PageID");
                intTabPosition = GetInt(row, "TabPosition");
                strTabTitle = GetString(row, "TabTitle");
                strTabDesc = GetString(row, "Description");
                dblC1Width = GetDouble(row, "C1Width");
                dblC2Width = GetDouble(row, "C2Width");
                dblC3Width = GetDouble(row, "C3Width");

                return base.MapData(row);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Tab--:--ClsTab.cs--:--MapData()--");
                VMuktiAPI.ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return false;
            }
        }

        public int Add_Tab(int intTabId, int intPageId, int intTabPosition, string strTabTitle, string strTabDesc, int intUserId, double C1Width, double C2Width, double C3Width)
        {
            try
            {
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    //ExecuteNonQuery(out cmd, "spAETabs",
                    //    CreateParameter("@pID", SqlDbType.BigInt, intTabId),
                    //    CreateParameter("@pPageID", SqlDbType.BigInt, intPageId),
                    //    CreateParameter("@pTabPosition", SqlDbType.Int, intTabPosition),
                    //    CreateParameter("@pTabTitle", SqlDbType.NVarChar, strTabTitle),
                    //    CreateParameter("@pDescription", SqlDbType.NVarChar, strTabDesc),
                    //    CreateParameter("@pGivenID", SqlDbType.BigInt, intUserId),
                    //    CreateParameter("@pC1Width", SqlDbType.Float, C1Width),
                    //    CreateParameter("@pC2Width", SqlDbType.Float, C2Width),
                    //    CreateParameter("@pC3Width", SqlDbType.Float, C3Width),
                    //    CreateParameter("@pReturnMaxID", SqlDbType.BigInt, ParameterDirection.Output));


                    List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                    clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                    objInfo.Direction = "Input";
                    objInfo.PName = "@pID";
                    objInfo.PValue = intTabId;
                    objInfo.PDBType = "BigInt";
                    objInfo.PSize = 200;

                    clsSqlParametersInfo objInfo2 = new clsSqlParametersInfo();
                    objInfo2.Direction = "Input";
                    objInfo2.PName = "@pPageID";
                    objInfo2.PValue = intPageId;
                    objInfo2.PDBType = "BigInt";
                    objInfo2.PSize = 200;


                    clsSqlParametersInfo objInfo3 = new clsSqlParametersInfo();
                    objInfo3.Direction = "Input";
                    objInfo3.PName = "@pTabPosition";
                    objInfo3.PValue = intTabPosition;
                    objInfo3.PDBType = "Int";
                    objInfo3.PSize = 200;

                    clsSqlParametersInfo objInfo4 = new clsSqlParametersInfo();
                    objInfo4.Direction = "Input";
                    objInfo4.PName = "@pTabTitle";
                    objInfo4.PValue = strTabTitle;
                    objInfo4.PDBType = "NVarChar";
                    objInfo4.PSize = 200;

                    clsSqlParametersInfo objInfo5 = new clsSqlParametersInfo();
                    objInfo5.Direction = "Input";
                    objInfo5.PName = "@pDescription";
                    objInfo5.PValue = strTabDesc;
                    objInfo5.PDBType = "NVarchar";
                    objInfo5.PSize = 200;

                    clsSqlParametersInfo objInfo6 = new clsSqlParametersInfo();
                    objInfo6.Direction = "Input";
                    objInfo6.PName = "@pGivenID";
                    objInfo6.PValue = intUserId;
                    objInfo6.PDBType = "BigInt";
                    objInfo6.PSize = 200;

                    clsSqlParametersInfo objInfo7 = new clsSqlParametersInfo();
                    objInfo7.Direction = "Input";
                    objInfo7.PName = "@pC1Width";
                    objInfo7.PValue = C1Width;
                    objInfo7.PDBType = "Float";
                    objInfo7.PSize = 200;

                    clsSqlParametersInfo objInfo8 = new clsSqlParametersInfo();
                    objInfo8.Direction = "Input";
                    objInfo8.PName = "@pC2Width";
                    objInfo8.PValue = C2Width;
                    objInfo8.PDBType = "Float";
                    objInfo8.PSize = 200;

                    clsSqlParametersInfo objInfo9 = new clsSqlParametersInfo();
                    objInfo9.Direction = "Input";
                    objInfo9.PName = "@pC3Width";
                    objInfo9.PValue = C3Width;
                    objInfo9.PDBType = "Float";
                    objInfo9.PSize = 200;

                    clsSqlParametersInfo objInfo10 = new clsSqlParametersInfo();
                    objInfo10.Direction = "Output";
                    objInfo10.PName = "@pReturnMaxID";
                    objInfo10.PValue = -1;
                    objInfo10.PDBType = "BigInt";
                    objInfo10.PSize = 200;

                    lstSP.Add(objInfo);
                    lstSP.Add(objInfo2);
                    lstSP.Add(objInfo3);
                    lstSP.Add(objInfo4);
                    lstSP.Add(objInfo5);
                    lstSP.Add(objInfo6);
                    lstSP.Add(objInfo7);
                    lstSP.Add(objInfo8);
                    lstSP.Add(objInfo9);
                    lstSP.Add(objInfo10);

                    clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                    CSqlInfo.objParam = lstSP;

                    return int.Parse(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteReturnNonQuery("spAETabs", CSqlInfo).ToString());
                }
                else
                {
                    return (new ClsTabDataService().AddTab(intTabId, intPageId, intTabPosition, strTabTitle, strTabDesc, intUserId, C1Width, C2Width, C3Width));
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Tab--:--ClsTab.cs--:--Add_Tab()--");
                VMuktiAPI.ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return -1;
            }
        }

        public int Get_TabMaxId()
        {
            try
            {
                //ExecuteNonQuery(out cmd, "spGMaxTabId",
                //    CreateParameter("@pReturnMaxID", SqlDbType.BigInt, ParameterDirection.Output));
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                    clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                    objInfo.Direction = "Output";
                    objInfo.PName = "@pReturnMaxID";
                    objInfo.PValue = -1;
                    objInfo.PDBType = "BigInt";
                    objInfo.PSize = 200;

                    clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                    CSqlInfo.objParam = lstSP;

                    return int.Parse(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteReturnNonQuery("spGMaxTabId", CSqlInfo).ToString());
                }
                else
                {
                    return (new ClsTabDataService().GetMaxTabId());
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Tab--:--ClsTab.cs--:--Get_TabMaxId()--");
                VMuktiAPI.ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return -1;
            }
        }

        public void Remove_Tab(int intTabId)
        {
            try
            {
                //ExecuteDataSet("spDTabs", CommandType.StoredProcedure, CreateParameter("@pID", SqlDbType.BigInt, intTabId));
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                    clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                    objInfo.Direction = "Output";
                    objInfo.PName = "@pID";
                    objInfo.PValue = intTabId;
                    objInfo.PDBType = "BigInt";
                    objInfo.PSize = 200;

                    clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                    CSqlInfo.objParam = lstSP;

                    VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spDTabs", CSqlInfo);
                }
                else
                {
                    new ClsTabDataService().RemoveTab(intTabId);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Tab--:--ClsTab.cs--:--Remove_Tab()--");
                VMuktiAPI.ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public DataSet Get_TabPage(int intPageId)
        {
            try
            {
                //return ExecuteDataSet("spGTabPage", CreateParameter("@pID", SqlDbType.Int, intPageId));
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    //ExecuteDataSet("spGTabPage", CreateParameter("@pID", SqlDbType.Int, intPageId));
                    List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                    clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                    objInfo.Direction = "Input";
                    objInfo.PName = "@pID";
                    objInfo.PValue = intPageId;
                    objInfo.PDBType = "Int";
                    objInfo.PSize = 200;

                    lstSP.Add(objInfo);

                    clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                    CSqlInfo.objParam = lstSP;

                    return VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGTabPage", CSqlInfo).dsInfo;
                }
                else
                {
                    return (new ClsTabDataService().GetTabPage(intPageId));
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Tab--:--ClsTab.cs--:--Get_TabPage()--");
                VMuktiAPI.ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        //public DataSet Get_Tab(int intTabId)
        //{
        //    if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
        //    {
        //        //return VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select * from Tabs where id=" + intTabId + "and isdeleted='False'").dsInfo;
        //        return null;
        //    }
        //    else
        //    {
        //        return new ClsTabDataService().GetTab(intTabId);
        //    }
        //}

        public static ClsTab Get_Tab(int intTabId)
        {
            try
            {
                ClsTab obj = new ClsTab();
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    //ExecuteDataSet("select * from Tabs where id=" + intTabid + "and isdeleted='False'", CommandType.Text, null);
                    //ExecuteDataSet("select id,pagetitle from page where ID=" + intPageId + "and isdeleted='false'", CommandType.Text, null)

                    DataSet ds = VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select * from Tabs where id=" + intTabId + "and isdeleted='False'").dsInfo;
                    if (!obj.MapData(ds)) obj = null;
                }
                else
                {
                    DataSet ds = new ClsTabDataService().GetTab(intTabId);
                    if (!obj.MapData(ds)) obj = null;
                }
                return obj;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Tab--:--ClsTab.cs--:--Get_Tab()--");
                VMuktiAPI.ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        #endregion
    }
}
